package com.erp.module.jiJian.shangChuanJiJian

import android.content.Context
import android.util.Log
import com.erp.bean.BenZuKBean
import com.erp.Interface.OnUserDataInterface
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import com.erp.module.jiJian.JiJianContract
import com.erp.module.jiJian.JiJianModel
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
 * Created by Administrator on 2017/11/13/013.
 * 作者：刘洋
 */
class IShuangChuanJiJianKP(val SCJJKV : JiJianContract.ShuangChanJiJianKView, val context : Context) : JiJianContract.ShuangChuanJiJianKP {
    val md : JiJianContract.JiJianMP= JiJianModel()
    var page=0
    override fun getXiaoZu() {
        md.getBenZu(context,object : OnUserDataInterface {
            override fun onUserData(data: String) {
                Log.d("TAAAG","data="+data)
                var xiaoZuKBeanList = ArrayList<BenZuKBean>()
                var zuhao=""
                try {
                    var jo= JSONObject(data)
                    val it = jo.keys()
                    while(it.hasNext()){
                        var key =it.next()
                        var xzjo= jo.getJSONObject(key)
                        var xiaoZuKBean= BenZuKBean()
                        xiaoZuKBean.wu_id= JsonUtil.getvolue(xzjo,"wu_id")
                        xiaoZuKBean.coefficient= JsonUtil.getvolue(xzjo,"coefficient")
                        xiaoZuKBean.coefficient_total= JsonUtil.getvolue(xzjo,"coefficient_total")
                        xiaoZuKBean.wu_name= JsonUtil.getvolue(xzjo,"wu_name")
                        xiaoZuKBean.work_number= JsonUtil.getvolue(xzjo,"work_number")
                        xiaoZuKBean.gangwei_name= JsonUtil.getvolue(xzjo,"gangwei_name")
                        zuhao=JsonUtil.getvolue(xzjo,"gnum")
                        xiaoZuKBeanList.add(xiaoZuKBean)

                    }
                }catch (e : JSONException){
                    e.printStackTrace()
                }
                Log.d("TAAAG","data="+data)
                SCJJKV.BenZuShuJuOk(xiaoZuKBeanList,zuhao)
            }

            override fun onNot(throwable: String) {
                SCJJKV.BenZuShuJuNo(throwable)
                Log.d("TAAAG","throwable="+throwable)
            }

        })
    }

    override fun setSaoMiao(sj :String ,jh : String ,zh : String, data :String) {
        var jo=JSONObject()
        try{
            jo.put("qr_code",data)
            if(sj != "") {
                jo.put("pw_time", Myutil.dateToStamp(sj))
            }
            if(jh!=""){
                jo.put("dnum", jh)
            }
            if(zh!=""){
                jo.put("ygzh", zh)
            }
        }catch (e : JSONException){
            e.printStackTrace()
        }
        md.setJiJianSaoMiao(context,jo.toString(),object : OnUserDataInterface{
            override fun onUserData(data: String) = SCJJKV.ShangChuanOk(data)

            override fun onNot(throwable: String) = SCJJKV.BenZuShuJuNo(throwable)

        })
    }
}